import 'dart:math';

import 'package:flutter/material.dart';

import '../../class/eq/eq_swipe_item.dart';
import '../../commont/button.dart';
import '../../model/model.dart';
import '../../tool/assets.dart';

class EqSwipe extends StatefulWidget {
  List<Map<String, List<SignModel>>> sign_list;
  bool enable;
  void Function(SignModel, String) onChange;
  void Function(SignModel) onChangeForSend;

  void Function(int) onSwipe;
  EqSwipe(
      {super.key,
      required this.sign_list,
      required this.enable,
      required this.onChange,
      required this.onChangeForSend,
      required this.onSwipe});

  @override
  State<EqSwipe> createState() => _EqSwipeState();
}

class _EqSwipeState extends State<EqSwipe> {
  final PageController _pageController = PageController();
  int _currentPage = 0;

  @override
  void dispose() {
    _pageController.dispose(); // 销毁控制器
    super.dispose();
  }

  ///左右滑动按钮
  Widget _get_btn(double h, bool isleft) {
    return SizedBox(
      height: h,
      width: 20,
      child: imgButton(
          groundColor: Color_bg_view,
          overColor: Color_bg_view,
          shadowRadius: 0,
          img: Image.asset(isleft ? assets.swipe_left : assets.swipe_right),
          onClick: () {
            setState(() {
              _pageController.animateToPage(
                _currentPage = isleft ? max(0, _currentPage - 1) : min(widget.sign_list.length - 1, _currentPage + 1),
                duration: const Duration(milliseconds: 300),
                curve: Curves.easeInOut,
              );
            });
            widget.onSwipe(_currentPage);
          }),
    );
  }

  ///初始化每一页的内容
  Widget _get_gridView(double width, double height, int index) {
    double w = 30;
    List<SignModel> list = widget.sign_list[index].values.first;
    return SizedBox(
        height: height,
        width: width,
        child: GridView.extent(
          padding: const EdgeInsets.only(left: 10, right: 10),
          maxCrossAxisExtent: w,
          mainAxisSpacing: 0,
          crossAxisSpacing: (width - 20 - list.length * w) / (list.length - 1),
          childAspectRatio: w / height,
          physics: const NeverScrollableScrollPhysics(),
          children: list.map(($0) {
            return EqSwipeItem(
              signModel: $0,
              enable: widget.enable,
              onChange: (tag) => widget.onChange($0, tag),
              onChangeForSend: () => widget.onChangeForSend($0),
            );
          }).toList(),
        ));
  }

  ///页面
  Widget _get_page(double width, double height, int page) {
    return SizedBox(
        width: width,
        height: height,
        child: Stack(
          children: [
            ///页面背景
            Center(child: Image.asset(widget.sign_list[page].keys.first, height: 90)),
            Center(child: _get_gridView(width, height, page)),
          ],
        ));
  }

  @override
  Widget build(BuildContext context) {
    double h = 172 * GlobalConfig.scaleH;
    return LayoutBuilder(builder: (BuildContext context, BoxConstraints constrains) {
      return SizedBox(
        height: h,
        child: Row(
          children: [
            ///左边翻页按钮
            _get_btn(h, true),

            ///page
            SizedBox(
              width: constrains.maxWidth - 40,
              child: PageView.builder(
                  controller: _pageController,
                  onPageChanged: (index) {
                    setState(() {
                      _currentPage = index;
                    });
                  },
                  itemBuilder: (BuildContext context, int index) {
                    return _get_page(constrains.maxWidth - 40, h, index);
                  },
                  itemCount: widget.sign_list.length),
            ),

            ///右边翻页按钮
            _get_btn(h, false),
          ],
        ),
      );
    });
  }
}
